【LeetCode 55】Jump Game 跳跃游戏


“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.”
Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 给定一个非负整数数组,你最初位于数组的第一个位置。
* 数组中的每个元素代表你在该位置可以跳跃的最大长度。
* 判断你是否能够到达最后一个位置。
*
* 不断更新你能到达的最大位置
*/
public class leetcode55 {
public boolean canJump (int[] nums) {
if (nums.length == 1)
return true;
int max = 0;
//注意是小于max
for (int i = 0; i <= max; i++) {
max = Math.max(max,nums[i]+i);
if (max >= nums.length-1)
return true;
}
return false;
}
}
Thanks!